package julialang.tech.backend.service;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MenuService {

  @Autowired
  private WxMpService wxMpService;

  public void createMenu() throws WxErrorException {
    WxMenu menu = new WxMenu();

    WxMenuButton button1 = new WxMenuButton();
    button1.setType("click");
    button1.setName("今日新闻");
    button1.setKey("NEWS_TODAY");

    WxMenuButton button2 = new WxMenuButton();
    button2.setName("菜单");
    // 二级菜单
    WxMenuButton subButton = new WxMenuButton();
    subButton.setType("view");
    subButton.setName("官网");
    subButton.setUrl("http://www.julialang.tech");
    button2.getSubButtons().add(subButton);

    menu.getButtons().add(button1);
    menu.getButtons().add(button2);

    wxMpService.getMenuService().menuCreate(menu);
  }
}
